/*
 * @(#)SelectTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import java.lang.reflect.InvocationTargetException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;


/**
  SelectTag.java

  <p>This class is to to addin javascript checing during input select onchange. </p>
  <p>Purpose to Override struts taglib for tracing form modification.<p>
  <p>Modification in "struts-html.tld"<br>
  <pre>
     [FROM]
     &lt;tagclass&gt;org.apache.struts.taglib.html.SelectTag&lt;/tagclass&gt;
     [TO]
     &lt;tagclass&gt;com.dcivision.framework.taglib.html.SelectTag&lt;/tagclass&gt;

     [ADDED]
     &lt;attribute&gt;
     &lt;name&gt;textOnly&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
  </pre>
  </p>

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   09/07/2003
    @version         $Revision: 1.13 $
*/

public class SelectTag extends org.apache.struts.taglib.html.SelectTag {

  public static final String REVISION = "$Revision: 1.13 $";

  protected boolean textOnly = false;

  public int doStartTag() throws JspException {
    switch (BaseHandler.commonRoutine(this, pageContext, this.getTextOnly())) {
      case BaseHandler.DIRECT_SKIP_BODY:
        return SKIP_BODY;
      case BaseHandler.DIRECT_EVAL_BODY_BUFFERED:
        return EVAL_BODY_BUFFERED;
      case BaseHandler.RETURN_SUPER_DOSTART:
        return super.doStartTag();
      default:
        return super.doStartTag();
    }
  }

  public int doEndTag() throws JspException {
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(this, FormTag.class);

    if (textOnly || (formTag!=null && formTag.getTextOnly())) {
      if (saveBody != null) {
        ResponseUtils.write(pageContext, saveBody);
      }
      return(EVAL_PAGE);
    } else {
      return(super.doEndTag());
    }
  }

  public void setTextOnly(boolean val){
    textOnly = val;
  }

  public boolean getTextOnly(){
    return textOnly;
  }

  /**
   * Calculate the match values we will actually be using.
   * @throws JspException
   */
  public void calculateMatchValues() throws JspException {
    if (this.value != null) {
      this.match = new String[1];
      this.match[0] = this.value;

    } else {
      Object bean = RequestUtils.lookup(pageContext, name, null);
      if (bean == null) {
        JspException e =
          new JspException(messages.getMessage("getter.bean", name));

        RequestUtils.saveException(pageContext, e);
        throw e;
      }

      try {
        this.match = BeanUtils.getArrayProperty(bean, property);
        if (this.match == null) {
          this.match = new String[0];
        }

      } catch (IllegalAccessException e) {
        RequestUtils.saveException(pageContext, e);
        throw new JspException(
          messages.getMessage("getter.access", property, name));

      } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        RequestUtils.saveException(pageContext, t);
        throw new JspException(
          messages.getMessage("getter.result", property, t.toString()));

      } catch (NoSuchMethodException e) {
        RequestUtils.saveException(pageContext, e);
        throw new JspException(
          messages.getMessage("getter.method", property, name));
      }
    }
  }
  
  public void release() {
    super.release();
    textOnly = false;
  }

}
